<?php
/*
 * Created on May 2, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
require_once("dao/IDao.php");
require_once("model/Course.php");
require_once("model/Venue.php");

class CourseDAO implements IDao{
	var $conn;
	
	public function __construct($conn){
		$this->conn =& $conn;
	}
	
	public function getItem($courseid){
		$course = null;
		try{
			if($stmt = $this->conn->prepare("CALL COURSE_READ_ITEM(?)")){
				$stmt->bind_param("i",$courseid);
				$stmt->execute();
				
				$stmt->bind_result($_courseid,$_venueid,$_venuename,$_coursename,$_deleted);
				
				while($stmt->fetch()){
					$course = new Course();
					$course->courseid = $_courseid;
					$course->venue = new Venue($_venueid,$_venuename);
					$course->coursename = $_coursename;
					$course->deleted = $_deleted;
				}
			}
			else{
				throw new Exception("Statement preparation failed in class:" . __CLASS__);
			}	
		}
		catch(Exception $e){
			throw new Exception("Statement preparation failed in class:" . __CLASS__."\t".$e->getMessage());
		}
		
		$this->conn->close();
		
		return $course;
	}
	
	public function searchCourses($venueId,$courseName,$deleted){
		$result = null;
		try{
			if($stmt = $this->conn->prepare("CALL COURSE_READ_LIST(?,?,?)")){
				$stmt->bind_param("isi",$venueId,$courseName,$deleted);
				$stmt->execute();
				
				$stmt->bind_result($_courseid,$_venueid,$_venuename,$_coursename,$_par,$_average,$_diff,$_cards,$_percentage1s,$_percentage2s,$_percentage3s,$_percentage4s,$_percentage5s,$_deleted);
				$result = array();
				while($stmt->fetch()){
					$course = Course::initWithParam($_courseid, $_coursename, $_par, $_average, $_diff, $_cards, $_percentage1s, $_percentage2s, $_percentage3s, $_percentage4s, $_percentage5s);
					$venue = new Venue($_venueid, $_venuename);
					$course->venue = $venue;
					$course->deleted = $_deleted;
					array_push($result,$course);
				}
			}
			else{
				throw  new Exception("Statement preparation failed in class:" . __CLASS__);
			}
		}
		catch (Exception $e){
			throw  new Exception("Statement prepartion failed in class:" . __CLASS__ . "<br/>" . $e->getMessage());
		}
		return $result;
	}
	
	public function saveItem($course){
		try{
			if($stmt = $this->conn->prepare("CALL COURSE_UPDATE(?,?,?,?)")){
				$stmt->bind_param("iisi",$course->courseid,$course->venue->venueid,$course->coursename,$course->deleted);
				$stmt->execute();
			}
			else{
				throw new Exception("Statement preparation failed in class:" . __CLASS__);
			}
		}
		catch(Exception $e){
			throw new Exception("Statement preparation failed in class:" . __CLASS__."<br/>".$e->getMessage());
		}
		$this->conn->close();
	}
	
	public function deleteItem($courseid){
		try{
			if($stmt = $this->conn->prepare("CALL COURSE_DELETE(?)")){
				$stmt->bind_result("i",$course->courseid);
				$stmt->execute();
			}
			else{
				throw new Exception("Statement preparation failed in class:" . __CLASS__);
			}
		}
		catch(Exception $e){
			throw new Exception("Statement preparation failed in class:" . __CLASS__."<br/>".$e->getMessage());
		}
		$this->conn->close();
	}
	
	public function __destruct(){}
}
?>
